package com.ms.player;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import com.xpec.c4.db.model.PlayerModel;
import com.xpec.c4.game.utility.GameUtility;


public class PlayerNickNameParser {
	private static final String mergerWord = "#";
	
	/** 玩家名稱後四碼的數字格式 */
	public static final String PLAYER_NAME_NUMBER_FORMAT = "0000";

	public static String mergeNickName(String name , String nameNumber){
		if(nameNumber == null || nameNumber.isEmpty() || nameNumber.length() == 1){
			return name;
		}
		return name + mergerWord + nameNumber;
	}
	
	public static String mergeNickName(PlayerModel playerModel){
		if(playerModel == null){
			return "";
		}
		
		String name = playerModel.getName();
		if(!GameUtility.isHaveString(name)){
			return "";
		}
		
		String nameNumber = playerModel.getNameNumber();
		if(nameNumber == null || nameNumber.isEmpty() || nameNumber.length() == 1){
			return name;
		}
		
		return name + mergerWord + nameNumber;
	}
	
	public static String[] dismantleNickName(String nickname){
		if(isMergeNickName(nickname)){
			return nickname.split(mergerWord);	
		}else{
			return new String[]{nickname , ""};
		}
	}

	/**
	 * 玩家名稱後面的數字轉成包含 0 的字串格式
	 * 
	 * @param nameNumber
	 *            玩家名稱編號
	 * @return
	 */
	public static String nameNumberToString(int nameNumber) {
		NumberFormat formatter = new DecimalFormat(PLAYER_NAME_NUMBER_FORMAT);
		String result = formatter.format(nameNumber);
		return result;
	}
	
	public static boolean isMergeNickName(String nickname){
		return nickname.contains(mergerWord);
	}
}
